package com.octant.testipc;

import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.home.sdk.bean.HomeBean;
import com.tuya.smart.home.sdk.callback.ITuyaGetHomeListCallback;
import com.tuya.smart.home.sdk.callback.ITuyaHomeResultCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.PropertyResourceBundle;

public class HomeUtil {

    public static final int HOME_STATE_INIT = 0;
    public static final int HOME_STATE_LIST_HOME_SUCCESS = 1;
    public static final int HOME_STATE_LIST_HOME_FAIL = 2;
    public static final int HOME_STATE_CREATE_HOME_SUCCESS = 3;
    public static final int HOME_STATE_CREATE_HOME_FAIL = 4;
    public static final int HOME_STATE_GET_HOME_DETAIL_SUCCESS = 5;
    public static final int HOME_STATE_GET_HOME_DETAIL_FAIL = 6;
    private int homeState = -1;
    public HomeBean homeBean;
    HomeUtilListener listener;

    private static HomeUtil instance = new HomeUtil();

    public static HomeUtil getInstance() {
        return instance;
    }

    public void initHome(){
        homeState = HOME_STATE_INIT;
        if (listener != null){
            listener.onHomeStateChange(homeState);
        }
        listHome();
    }

    public void createHome(String name, List<String> rooms){
        TuyaHomeSdk.getHomeManagerInstance().createHome(name, 0, 0, "ShenZhen", rooms, new ITuyaHomeResultCallback() {
            @Override
            public void onSuccess(HomeBean bean) {
                // do something
                homeState = HOME_STATE_CREATE_HOME_SUCCESS;
                if (listener != null){
                    listener.onHomeStateChange(homeState);
                }
                listHome();
            }
            @Override
            public void onError(String errorCode, String errorMsg) {
                // do something
                homeState = HOME_STATE_CREATE_HOME_FAIL;
                if (listener != null){
                    listener.onHomeStateChange(homeState);
                }
            }
        });
    }

    public void listHome(){
        TuyaHomeSdk.getHomeManagerInstance().queryHomeList(new ITuyaGetHomeListCallback() {
            @Override
            public void onSuccess(List<HomeBean> homeBeans) {
                // do something
                homeState = HOME_STATE_LIST_HOME_SUCCESS;
                if (listener != null){
                    listener.onHomeStateChange(homeState);
                }
                if (homeBeans==null || homeBeans.isEmpty()){
                    createHome("root",new ArrayList<String>());
                }else {
                    getHomeDetail(homeBeans.get(0).getHomeId());
                }
            }
            @Override
            public void onError(String errorCode, String error) {
                // do something
                homeState = HOME_STATE_LIST_HOME_FAIL;
                if (listener != null){
                    listener.onHomeStateChange(homeState);
                }
            }
        });
    }

    public void getHomeDetail(long id){
        TuyaHomeSdk.newHomeInstance(id).getHomeDetail(new ITuyaHomeResultCallback() {
            @Override
            public void onSuccess(HomeBean bean) {
                // do something
                homeBean = bean;
                homeState = HOME_STATE_GET_HOME_DETAIL_SUCCESS;
                if (listener != null){
                    listener.onHomeStateChange(homeState);
                }
            }
            @Override
            public void onError(String errorCode, String errorMsg) {
                // do something
                homeState = HOME_STATE_GET_HOME_DETAIL_FAIL;
                if (listener != null){
                    listener.onHomeStateChange(homeState);
                }
            }
        });
    }

    public interface HomeUtilListener{
        void onHomeStateChange(int state);
    }
}
